package com.halflife.comeElectricity.basic.address;

import com.halflife.comeElectricity.member.merchant.Merchant;
import com.halflife.shiro.utils.ShiroUtil;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
 * Created by Administrator on 2018/3/9 0009.
 */
@RestController
@RequestMapping("mc_address")
public class AddressController {

    @Resource
    private AddressGroupRepository addressGroupRepository;
    @Resource
    private AddressRepository addressRepository;

    @PostMapping("/storeAddress")
    @ResponseStatus(HttpStatus.CREATED)
    public ResponseEntity add(@RequestBody AddressVO addressVO,
                              HttpServletRequest request){
                    //获取当前用户
                    Merchant merchant  =  ShiroUtil.getMerchant(request);
                    //推送用户
                    HashMap<String, Object> param = new HashMap();
                   //清空Address
                   List<AddressGroup> gl = addressGroupRepository.selectByMerchantId(merchant.getId());
                   if(gl.size()>0)
                   {
                       for (AddressGroup a : gl){

                           addressRepository.deleteByGroupId(a.getId());
                       }
                   }
                   //清空AddressGroup
                   addressGroupRepository.deleteByMerchantId(merchant.getId());
                   //重新拼接我的常用地址
                   if(addressVO!=null){

                       if(addressVO.getStartPlace()!=null){

                           //保存AddreddGroup
                           AddressGroup g = new AddressGroup(0,merchant.getId());
                           //获取GroupID
                           addressGroupRepository.insert(g);
                           //保存起始地址
                           Address address_start = addressVO.getStartPlace();
                           address_start.setGroupId(g.getId());
                           addressRepository.insert(address_start);
                       }
                       if(addressVO.getDestination()!=null && addressVO.getDestination().size()>0){

                           for(Address d: addressVO.getDestination()){

                               //保存AddreddGroup
                               AddressGroup g = new AddressGroup(1,merchant.getId());
                               //获取GroupID
                               addressGroupRepository.insert(g);
                               d.setGroupId(g.getId());
                               addressRepository.insert(d);
                           }
                       }

                   }
                   return new ResponseEntity(1,HttpStatus.OK);
    }

    @GetMapping("/getAddByType")
    @ResponseStatus(HttpStatus.CREATED)
    public ResponseEntity<AddressVO> getMerchantAddress(HttpServletRequest request){

        //获取当前用户
        Merchant merchant  =  ShiroUtil.getMerchant(request);
        AddressVO v = new AddressVO();
        HashMap<String, Object> param = new HashMap();
        HashMap<String, Object> param_ = new HashMap();
        List<Address> l = new ArrayList<Address>();
            //获取起始地
            param.put("merchantId", merchant.getId());
            param.put("type", 0);
            List<AddressGroup> gl = addressGroupRepository.selectByMerIdAndType(param);
            if(gl.size()>0){

                Address a = addressRepository.selectByGroupId(gl.get(0).getId());
                if(a!=null)
                    v.setStartPlace(a);
            }
            //获取目的地
            param_.put("merchantId", merchant.getId());
            param_.put("type", 1);
            List<AddressGroup> gl_ = addressGroupRepository.selectByMerIdAndType(param_);
            if(gl.size()>0){

                for(AddressGroup g: gl_){

                    Address a = addressRepository.selectByGroupId(g.getId());
                    if(a!=null)
                        l.add(a);
                }
                v.setDestination(l);
            }
        return new ResponseEntity<AddressVO>(v,HttpStatus.OK);
    }
}
